package services

import (
	"TaskAssignmentSystem/internal/admin/services"
	"TaskAssignmentSystem/pkg/db/redis"
	"TaskAssignmentSystem/pkg/utils"
	"errors"
	"fmt"
	"time"
)

type PhoneService struct {
}

func (s *PhoneService) sendShortMessage(phone, randCode, sendKey, codeKey string, sendKeyTime, codeKeyTime time.Duration) error {
	/*
		if !utils.IsMobile(phone) {
			return errors.New("请填写正确的手机号码")
		}
	*/
	var dictServe services.SystemDictService
	if !dictServe.IsMobile(phone) {
		return errors.New("请填写正确的手机号码")
	}
	//一，验证发送时间间隔
	lastRes := redis.RedisGClient.Get(sendKey)
	if lastRes.Err() == nil {
		return errors.New("发送手机验证码过于频繁!")
	}
	err := redis.RedisGClient.Set(sendKey, phone, sendKeyTime).Err() // 允许发送手机验证码的隔时间
	if err != nil {
		return err
	}
	//二，保存验证码
	err = redis.RedisGClient.Set(codeKey, randCode, codeKeyTime).Err() // 手机验证码有效时间
	if err != nil {
		//验证码保存redis失败，删除redis发送时间设置
		redis.RedisGClient.Del(sendKey)
		return err
	}
	//三，发送验证码到手机
	err = utils.SendSms(phone, randCode)
	if err != nil {
		//验证码发送失败，删除redis配置
		redis.RedisGClient.Del(sendKey)
		redis.RedisGClient.Del(codeKey)
		return err
	}
	return nil
}

func (s *PhoneService) SendVerificationCodePhoneResetPwd(phone string) error {
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_phone_reset_pwd_send", phone)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_phone_reset_pwd_code", phone)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendShortMessage(phone, randCode, sendKey, codeKey, sendKeyTime, codeKeyTime)
}

func (s *PhoneService) SendVerificationCodeEmailResetOldPhone(phone string) error {
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_email_reset_old_phone_send", phone)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_email_reset_old_phone_code", phone)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendShortMessage(phone, randCode, sendKey, codeKey, sendKeyTime, codeKeyTime)
}

func (s *PhoneService) SendVerificationCodePhoneResetOldPhone(phone string) error {
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_phone_reset_old_phone_send", phone)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_phone_reset_old_phone_code", phone)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendShortMessage(phone, randCode, sendKey, codeKey, sendKeyTime, codeKeyTime)
}

func (s *PhoneService) SendVerificationCodePhoneResetNewPhone(phone string) error {
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_phone_reset_new_phone_send", phone)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_phone_reset_new_phone_code", phone)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendShortMessage(phone, randCode, sendKey, codeKey, sendKeyTime, codeKeyTime)
}
